package com.yxp.offer;

import com.yxp.util.TreeNode;

import java.util.*;

/**
 * @author : yang.xp
 * @date : 2021/8/18 11:53
 * @description : 剑指 Offer 32 - I. 从上到下打印二叉树
 * 从上到下打印出二叉树的每个节点，同一层的节点按照从左到右的顺序打印。 BFS
 * 例如:
 * 给定二叉树: [3,9,20,null,null,15,7],
 * [3,9,20,15,7]
 */
public class Problem32_1 {

    public static void main(String[] args) {

    }

    public int[] levelOrder(TreeNode root) {
        if (root == null) {
            return new int[0];
        }
        Queue<TreeNode> nodeQueue = new LinkedList<>();
        nodeQueue.add(root);
        List<Integer> arrayList = new ArrayList<Integer>();
        while (!nodeQueue.isEmpty()) {
            TreeNode poll = nodeQueue.poll();
            if (poll.left != null) {
                nodeQueue.add(poll.left);
            }
            if (poll.right != null) {
                nodeQueue.add(poll.right);
            }
            arrayList.add(poll.val);
        }
        int[] ant = new int[arrayList.size()];
        for (int i = 0; i < arrayList.size(); i++) {
            ant[i] = arrayList.get(i);
        }
        return ant;
    }










//    public int[] levelOrder(TreeNode root) {
//        if (root == null) return new int[0];
//        Queue<TreeNode> queue = new LinkedList<>();
//        queue.add(root);
//        ArrayList<Integer> ans = new ArrayList<>();
//        while (!queue.isEmpty()) {
//            TreeNode node = queue.poll();
//            ans.add(node.val);
//            if (node.left != null) queue.add(node.left);
//            if (node.right != null) queue.add(node.right);
//        }
//        int[] res = new int[ans.size()];
//        for (int i = 0; i < ans.size(); i++)
//            res[i] = ans.get(i);
//        return res;
//    }
}
